//纯后端的保存状态方案: session会话控制
//session: 把数据存储在服务器端
//session: 基于cookie 来存储sessionid  如果浏览器把cookie禁用了那么session也不能使用了
const express = require("express");
const session = require("express-session");
let app = express();
app.use(
  session({
    name: "mysession",
    secret: "myscret",
    resave: true,
    saveUninitialized: true,
    cookie: {
      maxAge: 3600 * 1000,
      httpOnly: true,
    },
  })
);
//所有操作都会基于req
//服务器重启会导致session丢失

//
//设置session
app.get("/setsession", (req, res) => {
  req.session.myname = "hello"; //设置session
  res.send("设置session");
});

//获取session
app.get("/getsession", (req, res) => {
  console.log(req.session);
  let str = req.session.myname;
  res.send("设置session" + str);
});

//删除session
app.get("/delsession", (req, res) => {
  req.session.destroy(); //删除所有的session
  res.send("删除session");
});

app.listen(8989);
